﻿Namespace ControlLibrary
    ''' <summary>Used to handle the print settings for the standard dbiDayView controls.</summary>
    Public Class dbiDayViewPrintSettings

        Private l_LandscapeRequired As Boolean = False

        ''' <summary>Determines if the report must be printed in landscape mode.</summary>
        ''' <remarks>In order to preserve formatting and column integrity this value
        ''' will return true if the extra width is required.</remarks>
        Public Property LandscapeRequired() As Boolean
            Get
                Return l_LandscapeRequired
            End Get
            Set(ByVal value As Boolean)
                l_LandscapeRequired = value

                If value Then
                    Me.CheckBoxPrintLandscape.Checked = True
                    Me.CheckBoxPrintLandscape.Enabled = False
                End If
            End Set
        End Property

        ''' <summary>
        ''' Loads the dbiDayView current settings as well as a predefined PrinterSettings.
        ''' </summary>
        ''' <param name="curDayView">The dbiDayView to get the settings from.</param>
        ''' <param name="curPrinterSettings">A system.drawing.printing.printersettings object to set defaults from.</param>
        ''' <remarks></remarks>
        Public Sub LoadSettingsFromDayView(ByRef curDayView As Dbi.WinControl.DayView.dbiDayView, ByRef curPrinterSettings As System.Drawing.Printing.PrinterSettings)

            'Set the various controls based on the current settings of the dbiDayView.
            Me.TextBoxTitle.Text = curDayView.PrintTitle
            Me.TextBoxSubTitle.Text = curDayView.PrintSubTitle
            Me.TextBoxPageText.Text = curDayView.PrintPageText

            Me.DbiLabelFont.Font = curDayView.PrintFont

            If LandscapeRequired = False Then
                Me.CheckBoxPrintLandscape.Checked = curPrinterSettings.DefaultPageSettings.Landscape
            End If
            Me.CheckBoxPrintColor.Checked = curPrinterSettings.DefaultPageSettings.Color

            Me.NumericUpDownDisplayColumns.Value = curDayView.PrintDisplayColumns
            Me.NumericUpDownTimeStart.Value = curDayView.PrintTimeStart
            Me.NumericUpDownTimeEnd.Value = curDayView.PrintTimeEnd

            Me.DbiLabelFont.Font = curDayView.PrintFont

            Me.ComboBoxFooterAlign.DataSource = System.Enum.GetValues(GetType(System.Drawing.ContentAlignment))
            Me.ComboBoxFooterAlign.SelectedItem = curDayView.PrintFooterAlign

            Me.ComboBoxPageTitleAlign.DataSource = System.Enum.GetValues(GetType(System.Drawing.ContentAlignment))
            Me.ComboBoxPageTitleAlign.SelectedItem = curDayView.PrintTitleAlign

            Me.ComboBoxReportType.DataSource = System.Enum.GetValues(GetType(Dbi.WinControl.enumPrintReportType))
            Me.ComboBoxReportType.SelectedItem = curDayView.PrintReportType

            Me.NumericUpDownMarginBottom.Value = curPrinterSettings.DefaultPageSettings.Margins.Bottom
            Me.NumericUpDownMarginLeft.Value = curPrinterSettings.DefaultPageSettings.Margins.Left
            Me.NumericUpDownMarginRight.Value = curPrinterSettings.DefaultPageSettings.Margins.Right
            Me.NumericUpDownMarginTop.Value = curPrinterSettings.DefaultPageSettings.Margins.Top

            Me.Refresh()
        End Sub

        ''' <summary>
        ''' Sets the dbiDayView and PrinterSettings object with the values set in the control.
        ''' </summary>
        ''' <param name="curDayView">The dbiDayView to apply the values set herein.</param>
        ''' <param name="curPrinterSettings">The system.drawing.printing.printersettings object to apply the values set herein.</param>
        Public Sub ApplySettingsToDayView(ByRef curDayView As Dbi.WinControl.DayView.dbiDayView, ByRef curPrinterSettings As System.Drawing.Printing.PrinterSettings)

            curDayView.PrintTitle = Me.TextBoxTitle.Text
            curDayView.PrintSubTitle = Me.TextBoxSubTitle.Text
            curDayView.PrintPageText = Me.TextBoxPageText.Text

            curDayView.PrintFont = Me.DbiLabelFont.Font

            curPrinterSettings.DefaultPageSettings.Landscape = Me.CheckBoxPrintLandscape.Checked
            curPrinterSettings.DefaultPageSettings.Color = Me.CheckBoxPrintColor.Checked

            curDayView.PrintDisplayColumns = Me.NumericUpDownDisplayColumns.Value
            curDayView.PrintTimeStart = Me.NumericUpDownTimeStart.Value
            curDayView.PrintTimeEnd = Me.NumericUpDownTimeEnd.Value

            curDayView.PrintFont = Me.DbiLabelFont.Font

            curDayView.PrintFooterAlign = Me.ComboBoxFooterAlign.SelectedItem

            curDayView.PrintTitleAlign = Me.ComboBoxPageTitleAlign.SelectedItem

            curDayView.PrintReportType = Me.ComboBoxReportType.SelectedItem

            curPrinterSettings.DefaultPageSettings.Margins.Bottom = Me.NumericUpDownMarginBottom.Value
            curPrinterSettings.DefaultPageSettings.Margins.Left = Me.NumericUpDownMarginLeft.Value
            curPrinterSettings.DefaultPageSettings.Margins.Right = Me.NumericUpDownMarginRight.Value
            curPrinterSettings.DefaultPageSettings.Margins.Top = Me.NumericUpDownMarginTop.Value
        End Sub

        ''' <summary>Displays a font dialog if the font label is clicked.</summary>
        Private Sub FontLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DbiLabelFont.Click
            FontDialog1.Font = sender.font
            FontDialog1.ShowDialog()
            sender.font = FontDialog1.Font
        End Sub

        ''' <summary>Enables/disables the timespan group box based on the combobox report type.</summary>
        Private Sub ComboBoxReportType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxReportType.SelectedIndexChanged
            If ComboBoxReportType.SelectedItem = Dbi.WinControl.enumPrintReportType.Page Then
                Me.GroupBox2.Enabled = True
            Else
                Me.GroupBox2.Enabled = False
            End If
        End Sub

        ''' <summary>Ensures the end time is greater than the start time for the TimeSpans</summary>
        Private Sub NumericUpDownTimeStart_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownTimeStart.ValueChanged, NumericUpDownTimeEnd.ValueChanged
            If NumericUpDownTimeStart.Value >= NumericUpDownTimeEnd.Value Then
                If NumericUpDownTimeStart.Value <> 0 Then
                    NumericUpDownTimeEnd.Value = NumericUpDownTimeStart.Value + 5
                End If
            End If
        End Sub
    End Class
End Namespace
